﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
 

namespace Sale_Operation
{
    public class CustomerOp : Component
    {
        /// <summary>
        /// 根据条件获取基础数据信息
        /// </summary>
        /// <param name="strCondition"></param>
        /// <param name="iCurrentPageIndex"></param>
        /// <param name="iRecordCount"></param>
        /// <returns></returns>
        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {                   
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT AssociationId,AssociationName,AssociationCost FROM Association_Info ");
                //sbSql.Append(" LEFT JOIN BaseData B ON (A.Cust_BData_Id=B.BData_Id)");
                //sbSql.Append(" LEFT JOIN BaseData C ON (A.Cust_Professional=C.BData_Id)");
                sbSql.Append(" WHERE 1=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY AssociationId ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "专业列表加载失败！["+ex.ToString()+"]", EMenuList.客户管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
   
        public override bool Save(object obj)
        { 
            bool blRes = false;
            Customer SM = obj as Customer;        
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                if (SM.Cust_Id == 0)
                {
                    sbSql.Append("INSERT INTO Association_Info (AssociationName,AssociationCost) SELECT ");
                    //sbSql.Append("Cust_Code='" + ValueHandler.GetStringValue(SM.Cust_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AssociationName='" + ValueHandler.GetStringValue(SM.Cust_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_BData_Id='" + ValueHandler.GetStringValue(SM.Cust_BData_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_Sex='" + ValueHandler.GetStringValue(SM.Cust_Sex, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_IdentityCode='" + ValueHandler.GetStringValue(SM.Cust_IdentityCode, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_Tel1='" + ValueHandler.GetStringValue(SM.Cust_Tel1, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AssociationCost=" + ValueHandler.GetDecNumberValue(SM.Cust_CompanyAddress));
                    //sbSql.Append("Email='" + ValueHandler.GetStringValue(SM.Email, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("QQ='" + ValueHandler.GetStringValue(SM.QQ, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_FamilyAddress='" + ValueHandler.GetStringValue(SM.Cust_FamilyAddress, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_CompanyAddress='" + ValueHandler.GetStringValue(SM.Cust_CompanyAddress, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_Professional='" + ValueHandler.GetStringValue(SM.Cust_Professional, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    //sbSql.Append("Cust_Memo='" + ValueHandler.GetStringValue(SM.Cust_Memo, ValueHandler.CharHandlerType.IsRepChar) + "',");           
                    //sbSql.Append("State=1,");
                    //sbSql.Append("CreateTime=getdate(),");
                    //sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.Cust_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增会员职务信息[" + ValueHandler.GetStringValue(SM.Cust_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.客户管理);

                }
                else
                {
                    sbSql.Append("UPDATE Customer SET ");
                    sbSql.Append("Cust_Code='" + ValueHandler.GetStringValue(SM.Cust_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_Name='" + ValueHandler.GetStringValue(SM.Cust_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_BData_Id='" + ValueHandler.GetStringValue(SM.Cust_BData_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_Sex='" + ValueHandler.GetStringValue(SM.Cust_Sex, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_IdentityCode='" + ValueHandler.GetStringValue(SM.Cust_IdentityCode, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_Tel1='" + ValueHandler.GetStringValue(SM.Cust_Tel1, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_Tel2='" + ValueHandler.GetStringValue(SM.Cust_Tel2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Email='" + ValueHandler.GetStringValue(SM.Email, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("QQ='" + ValueHandler.GetStringValue(SM.QQ, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_FamilyAddress='" + ValueHandler.GetStringValue(SM.Cust_FamilyAddress, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_CompanyAddress='" + ValueHandler.GetStringValue(SM.Cust_CompanyAddress, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_Professional='" + ValueHandler.GetStringValue(SM.Cust_Professional, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Cust_Memo='" + ValueHandler.GetStringValue(SM.Cust_Memo, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE Cust_Id=" + SM.Cust_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改客户信息[" + ValueHandler.GetStringValue(SM.Cust_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.客户管理);
                }
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改客户信息出错！[" + ex.ToString() + "]", EMenuList.客户管理);
            }
            db.Close();
            return blRes;
        }
        public override bool HasExists(object obj)
        {
            Customer SM = obj as Customer;          
            bool blRes = true;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM Customer WHERE Cust_Code='" + SM.Cust_Code + "' AND Cust_Id<>" + SM.Cust_Id + " AND State=1 ");
                sbSql.Append(" UNION ALL ");
                sbSql.Append(" SELECT * FROM Customer WHERE Cust_Name='" + SM.Cust_Name + "' AND Cust_Id<>" + SM.Cust_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }
        public override bool Delete(object obj)
        {
            Customer SM = obj as Customer;            
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("DELETE FROM Association_Info ");
                //sbSql.Append("State=0");
                sbSql.Append("WHERE AssociationId=" + SM.Cust_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除客户信息[" + ValueHandler.GetStringValue(SM.Cust_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.客户管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除客户信息出错！[" + ex.ToString() + "]", EMenuList.客户管理);
            }
            db.Close();
            return blRes;
        }
        public override object GetModel(object obj)
        {
            Customer SM = obj as Customer;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT AssociationId,AssociationName,AssociationCost FROM Association_Info ");
                //sbSql.Append(" LEFT JOIN BaseData B ON (A.Cust_BData_Id=B.BData_Id)");
                //sbSql.Append(" LEFT JOIN BaseData C ON (A.Cust_Professional=C.BData_Id)");
                sbSql.Append(" WHERE AssociationId=" + SM.Cust_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    //SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    //SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    //SM.Cust_BData_Id = ValueHandler.GetIntNumberValue(dr["Cust_BData_Id"]);
                    SM.Cust_Code = ValueHandler.GetStringValue(dr["AssociationId"]);
                    //SM.Cust_CompanyAddress = ValueHandler.GetStringValue(dr["Cust_CompanyAddress"]);
                    //SM.Cust_FamilyAddress = ValueHandler.GetStringValue(dr["Cust_FamilyAddress"]);
                    //SM.Cust_IdentityCode = ValueHandler.GetStringValue(dr["Cust_IdentityCode"]);
                    //SM.Cust_Memo = ValueHandler.GetStringValue(dr["Cust_Memo"]);
                    SM.Cust_Name = ValueHandler.GetStringValue(dr["AssociationName"]);
                    //SM.Cust_Professional = ValueHandler.GetIntNumberValue(dr["Cust_Professional"]);
                    //SM.Cust_Sex = ValueHandler.GetIntNumberValue(dr["Cust_Sex"]);
                    SM.Cust_Tel1 = ValueHandler.GetStringValue(dr["AssociationCost"]);
                    //SM.Cust_Tel2 = ValueHandler.GetStringValue(dr["Cust_Tel2"]);
                    //SM.Email = ValueHandler.GetStringValue(dr["Email"]);
                    //SM.QQ = ValueHandler.GetStringValue(dr["QQ"]);
                    //SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "客户列表加载失败！[" + ex.ToString() + "]", EMenuList.客户管理);
            }
            return SM;
        }
        
      
    }
}
